import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.ImageIO;

public class ImageEncryptor {
    private static final int XOR_KEY = 123456; // XOR加密密钥

    public static void encryptImage(String inputImagePath, String outputImagePath) throws IOException {
        BufferedImage image = ImageIO.read(new File(inputImagePath));
        for (int y = 0; y < image.getHeight(); y++) {
            for (int x = 0; x < image.getWidth(); x++) {
                int rgba = image.getRGB(x, y);
                int alpha = (rgba >> 24) & 0xff;
                int red = (rgba >> 16) & 0xff;
                int green = (rgba >> 8) & 0xff;
                int blue = rgba & 0xff;

                red ^= XOR_KEY;
                green ^= XOR_KEY;
                blue ^= XOR_KEY;

                int newRgba = (alpha << 24) | (red << 16) | (green << 8) | blue;
                image.setRGB(x, y, newRgba);
            }
        }
        ImageIO.write(image, "jpg", new File(outputImagePath));
    }

    public static void decryptImage(String inputImagePath, String outputImagePath) throws IOException {
        encryptImage(inputImagePath, outputImagePath); // 由于是XOR加密，解密和加密方法相同
    }
}
